package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"z-blog/e"
	"z-blog/models"
)

func GetCommentByArticle(c *gin.Context) {
	maps := make(map[string]interface{})
	data := make(map[string]interface{})

	articleId := c.Query("article_id")
	code := e.SUCCESS

	maps["article_id"] = articleId
	data["list"] = models.SelectComment(maps)

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

func AddComment(c *gin.Context) {
	data := make(map[string]interface{})
	code := e.SUCCESS

	data["OwnerId"] = c.Query("owner_id")
	data["Text"] = c.Query("text")
	data["ArticleId"], _ = strconv.Atoi(c.Query("article_id"))

	if models.InsertComment(data) != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": nil,
	})
}

func DelComment(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	code := e.SUCCESS

	if models.DelComment(id) != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": nil,
	})
}
